package com.prs.crm.action.oa;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Affiche;
import com.prs.crm.domain.oa.AfficheAccept;
import com.prs.crm.domain.oa.AfficheCollect;
import com.prs.crm.domain.oa.NoticeAccept;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.oa.AfficheAcceptService;
import com.prs.crm.service.oa.AfficheCollectService;
import com.prs.crm.service.oa.AfficheService;

public class AfficheAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Affiche affiche;
	private AfficheService afficheService;
	private PaginationSupport pagination;
	private Integer[] afficheIds;
	private String currentDate;
	private Integer[] acceptIds;
	private File upload;
	private String uploadFileName;
	private String accepts;
	private PersonService personService;
	private AfficheAcceptService afficheAcceptService;
	private AfficheCollect afficheCollect;
	private AfficheCollectService afficheCollectService;
	private AfficheAccept  afficheAccept;
	private String type;
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	@Action("listAffiche")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Affiche.class);
		criteria.add(Restrictions.eq("type", "公告"));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAfficheService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listActivity")
	public String listActivity() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Affiche.class);
		criteria.add(Restrictions.eq("type", "活动"));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAfficheService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action(value = "removeAfficheAccept", results = {
			@Result(name = "success", type = "redirect", location = "listReceiveNews"),
			@Result(name = "error", type = "chain", location = "listReceiveNews") })
	public String removeAfficheAccept() {
		this.afficheAcceptService.batchRemove(afficheIds);
		return SUCCESS;
	}
	@Action("listNews")
	public String listNews() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Affiche.class);
		criteria.add(Restrictions.eq("type", "新闻"));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAfficheService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("viewAfficheAccept")
	public String viewAfficheAccept() {
		if(this.afficheIds!=null){
			this.afficheAccept=this.afficheAcceptService.get(afficheIds[0]);
		}
		if(afficheAccept.getState().equals("未读")){
			afficheAccept.setState("已读");
			this.afficheAcceptService.submit(afficheAccept);
		}
		return SUCCESS;
	}
	@Action("editAffiche")
	public String edit() {
		this.setCurrentDate(ToolKits.currentDate());
		return SUCCESS;
	}

	@Action("alterAffiche")
	public String alterAffiche() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Affiche.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAfficheService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewAffiche")
	public String viewAffiche() {
		
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveAffiche", results = {
			@Result(name = "success", type = "redirect", location = "editAffiche"),
			@Result(name = "error", type = "chain", location = "editAffiche") })
	public String save() {
		if (affiche.getTitle() == null || "".equals(affiche.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (affiche.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (affiche.getCreattime() == null) {
			addActionError("发布时间不能为空");
			return ERROR;
		}
		this.setPrompt("公告提交成功!");
		this.getAfficheService().saveAffiche(affiche, upload, uploadFileName);
		return SUCCESS;
	}
	
	
	@Action(value = "removeAffiche", results = {
			@Result(name = "success", type = "redirect", location = "listHaveNews"),
			@Result(name = "error", type = "chain", location = "listHaveNews") })
	public String remove() {
		if (afficheIds == null) {
			this.addActionError("请选择要删除的公告");
			return ERROR;
		}
		this.getAfficheService().batchRemove(afficheIds);
		return SUCCESS;
	}
	@Action(value = "deleteAffiche", results = {
			@Result(name = "success", type = "redirect", location = "listHaveNews"),
			@Result(name = "error", type = "chain", location = "editNews") })
	public String deleteAffiche() {
		this.getAfficheService().remove(this.affiche);
		return SUCCESS;
	}
	
	
	public Affiche getAffiche() {
		return affiche;
	}

	public void setAffiche(Affiche affiche) {
		this.affiche = affiche;
	}

	public AfficheService getAfficheService() {
		return afficheService;
	}

	public void setAfficheService(AfficheService afficheService) {
		this.afficheService = afficheService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getAfficheIds() {
		return afficheIds;
	}

	public void setAfficheIds(Integer[] afficheIds) {
		this.afficheIds = afficheIds;
	}

	public void setCurrentDate(String currentDate) {
		this.currentDate = currentDate;
	}

	public String getCurrentDate() {
		return currentDate;
	}

	public String getFileName() {
		return uploadFileName;
	}

	public void setFileName(String fileName) {
		this.uploadFileName = fileName;
	}

	public String getAccepts() {
		return accepts;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public AfficheAcceptService getAfficheAcceptService() {
		return afficheAcceptService;
	}

	public void setAfficheAcceptService(AfficheAcceptService afficheAcceptService) {
		this.afficheAcceptService = afficheAcceptService;
	}

	public AfficheCollect getAfficheCollect() {
		return afficheCollect;
	}

	public void setAfficheCollect(AfficheCollect afficheCollect) {
		this.afficheCollect = afficheCollect;
	}

	public AfficheCollectService getAfficheCollectService() {
		return afficheCollectService;
	}

	public void setAfficheCollectService(AfficheCollectService afficheCollectService) {
		this.afficheCollectService = afficheCollectService;
	}

	public AfficheAccept getAfficheAccept() {
		return afficheAccept;
	}

	public void setAfficheAccept(AfficheAccept afficheAccept) {
		this.afficheAccept = afficheAccept;
	}

	public Integer[] getAcceptIds() {
		return acceptIds;
	}

	public void setAcceptIds(Integer[] acceptIds) {
		this.acceptIds = acceptIds;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
